﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using BCNPCObject;
using bus_TienichNPC;
using BaocaoNPC;
using BaocaoNPC.common;

namespace BaocaoNPC.Usercontrols
{
    public partial class UC_Report : UserControl
    {
        #region variable
            CN_THEODOINO_bus THEODOINO_bus = new CN_THEODOINO_bus();
            DataSet ds_BBTHUNGAN = new DataSet();
            public String strMadviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
            public string strUsername = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "NPC_ATM";
            public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
            public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
            DataSet dsResult = new DataSet();
            DataSet ds_Ketqua = new BCNPCEntities.EXCEL.ds_CN();
            int In = 1;            
        #endregion

        public UC_Report()
        {
            InitializeComponent();
            LOAD_DATA();
            Kiemtra();       
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            string strMatngan = strUsername;                 
        }

        private void Kiemtra()
        {
            if (rbtDS_TonCuoiThang.IsChecked == true)
            {
                grid1.IsEnabled = true;
            }
            else
            {
                grid1.IsEnabled = false;
            }
        }

        private void txtGiaTri_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (txtGiaTri.Text.Trim() == "")
                    {
                        MessageBox.Show("Không có giá trị tìm kiếm!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                        return;
                    }
                    else
                    {
                        ds_BBTHUNGAN.Tables[0].DefaultView.RowFilter = "MA_SOGCS = '" + txtGiaTri.Text.Trim().ToUpper() + "'";
                        if (ds_BBTHUNGAN.Tables[0].DefaultView.Count == 0)
                        {
                            MessageBox.Show("Mã sổ không có giá trị tìm kiếm!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        Find(trvBBAN, txtGiaTri.Text.ToUpper());
                    }
                }
            }
            catch(Exception ex)
            { }
        }

        private void Find(TreeView mainTreeView, string findToText)
        {
            foreach (object item in mainTreeView.Items)
            {
                TreeViewItem treeItem = mainTreeView.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                
                if (treeItem != null)
                {
                    FindAll(treeItem, findToText);
                }
                if (item != null)
                {
                    CheckBox chkNode = (CheckBox)treeItem.Header;
                    string[] m = chkNode.Tag.ToString().Split('@');
                    if (isContains(chkNode.Content.ToString(), findToText))
                    {
                        chkNode.IsChecked = true;
                        fill_Grid(Convert.ToInt32(m[1].ToString()), m[0].ToString().Trim());                        
                    }                    
                }
            }
        }

        void FindAll(ItemsControl items, string findToText)
        {
            foreach (object obj in items.Items)
            {
                ItemsControl childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
                if (childControl != null)
                {
                    FindAll(childControl, findToText); // Recursion
                }
                TreeViewItem item = childControl as TreeViewItem;

                if (item != null)
                {
                    CheckBox chkNode = (CheckBox)item.Header;
                    string[] m = chkNode.Tag.ToString().Split('@');
                    if (isContains(chkNode.Content.ToString(), findToText))
                    {
                        chkNode.IsChecked = true;
                        fill_Grid(Convert.ToInt32(m[1].ToString()), m[0].ToString().Trim());
                    }                    
                }
            }
        }

        private bool isContains(string a, string b)
        {
            if (a.ToUpper().Trim() == b.ToUpper().Trim())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void fill_Grid(Int32 sbb, string ma_so)
        {
            try
            {       
                dsResult = new DataSet();
                dsResult = GlobalServices.Service_NPCUS.CN_So_BBGiao_Nutcon(strUsername.ToUpper(), sbb, ma_so, strMadviqly);

                if (ds_Ketqua.Tables.Count == 0)
                {
                    if (ds_Ketqua != null)
                    {
                        copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                        ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";
                    }
                }
                else if (ds_Ketqua.Tables["CN"].Rows.Count == 0)
                {
                    if (dsResult != null)
                    {
                        copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                        ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";
                    }
                }
                else
                {
                    copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                    ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";
                }
            }
            catch (Exception ex)
            { }
        }

        private void LOAD_DATA()
        {
            try
            {
                ds_BBTHUNGAN = new DataSet();
                ds_BBTHUNGAN = GlobalServices.Service_NPCUS.CN_Select(strUsername.ToUpper(), strMadviqly);
                if (ds_BBTHUNGAN.Tables[0].Rows.Count>0)
                {
                    dsResult = ds_BBTHUNGAN;
                    fill_dulieu(ds_BBTHUNGAN);
                    Update_autotext();
                    txtGiaTri.FocusAutoText();
                }
            }
            catch (Exception ex)
            { }
        }

        private void Update_autotext()
        {
            try
            {
                if (ds_BBTHUNGAN.Tables.Count == 0)
                {
                    txtGiaTri.ClearItem();
                    return;
                }
                string ma_so = "";
                DataTable dt = new DataTable();
                //DataTable desiredResult = GroupBy("TeamID", "MemberID", dt);
                DataColumn Col1 = dt.Columns.Add("MA_SOGCS", typeof(String));
                dt.Columns.Add("TEN_SOGCS", typeof(String));
                //DataRow[] rgINS = ds_autotext.Tables[0].Select("group by MA_SOGCS AND avg(MA_SOGCS)");
                var result = (from row in ds_BBTHUNGAN.Tables[0].AsEnumerable()
                              group row by row["MA_SOGCS"] into temp
                              select new
                              {
                                  MA_SOGCS = temp.Key.ToString(),
                                  TEN_SOGCS = "",
                              }).ToList();

                //DataRow[] results = ds.Tables[0].Select("group by columnName AND avg(columnName)");
                for (int i = 0; i < result.Count; i++)
                {
                    //dt.Rows.Clear();
                    DataRow dr = dt.NewRow();
                    dr["MA_SOGCS"] = result[i].MA_SOGCS;
                    dr["TEN_SOGCS"] = result[i].TEN_SOGCS;
                    dt.Rows.Add(dr);
                    dt.AcceptChanges();
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.AcceptChanges();
                ProcessData(ds);
                txtGiaTri.ClearItem();
                if (ds != null) txtGiaTri.AddItem(new CMIS2_TextBox.AutoCompleteEntry(ds.Tables[0], "DataTable"));
            }
            catch
            { }
        }

        private void ProcessData(DataSet ds)
        {
            try
            {
                foreach (DataTable dt in ds.Tables)
                {
                    if (dt.Columns.Count > 0)
                    {
                        dt.Columns.AddRange(new DataColumn[] { new DataColumn("SEARCH", typeof(string)), new DataColumn("MA_SEARCH", typeof(string)), new DataColumn("TEN_SEARCHED", typeof(string)) });
                        foreach (DataRow dr in dt.Rows)
                        {
                            dr["SEARCH"] = dr["MA_SOGCS"].ToString() + dr["TEN_SOGCS"].ToString();
                            dr["MA_SEARCH"] = dr["SEARCH"];
                            dr["TEN_SEARCHED"] = dr["TEN_SOGCS"];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void fill_dulieu(DataSet ds)
        {            
            if (ds.Tables.Count == 0) return;
            TreeViewItem superNode = new TreeViewItem();
            TreeViewItem _node = new TreeViewItem();
            TreeViewItem _childnode = new TreeViewItem();
            CheckBox chkNode = new CheckBox();
            CheckBox chkChildNode = new CheckBox();   
            this.trvBBAN.Items.Clear();
            DataView dv = new DataView(ds.Tables[0]);
            dv.Sort = "SO_BBGIAO, MA_SOGCS";
            if (dv.Count == 0)
            {
                superNode.Header = "Không có dữ liệu biên bản";
                superNode.Tag = "Không có dữ liệu biên bản@";
                trvBBAN.Items.Add(superNode);
                return;
            }
            CheckBox chkSuperNode = new CheckBox();
            chkSuperNode.Content = "Tất cả biên bản";
            chkSuperNode.Tag = "@N@NUTGOC";
            chkSuperNode.ToolTip = "Biên bản";
            chkSuperNode.IsChecked = false;
            chkSuperNode.Click += new RoutedEventHandler(this.CheckBox_Click);
            superNode = new TreeViewItem();
            superNode.Header = chkSuperNode;
            superNode.IsExpanded = true;            
            trvBBAN.Items.Add(superNode);
            string strnutcha = "";
            string strnutcon = "";
            string str_bienban = "";
            string str_ma_sogcs = "";
            foreach (DataRowView dr in dv)
            {
                str_bienban = dr["SO_BBGIAO"].ToString();                
                str_ma_sogcs = dr["MA_SOGCS"].ToString();
                #region "parentnode"
                if (str_bienban != strnutcha)
                {
                    chkNode = new CheckBox();
                    chkNode.Content = str_bienban;
                    chkNode.Tag = str_bienban + "@" + chkSuperNode.ToolTip.ToString() + "@NUTCHA";
                    chkNode.ToolTip = str_bienban;
                    chkNode.IsChecked = false;
                    chkNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                    _node = new TreeViewItem();
                    _node.Header = chkNode;
                    _node.IsExpanded = false;
                    superNode.Items.Add(_node);
                    strnutcha = str_bienban;
                }
                # endregion
                #region childNode
                if (str_ma_sogcs != strnutcon)
                {
                    chkChildNode = new CheckBox();
                    chkChildNode.Content = str_ma_sogcs;
                    chkChildNode.Tag = str_ma_sogcs + "@" + chkNode.ToolTip.ToString() + "@NUTCON";
                    chkChildNode.ToolTip = str_ma_sogcs;
                    chkChildNode.IsChecked = false;
                    chkChildNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                    _childnode = new TreeViewItem();
                    _childnode.Header = chkChildNode; //+ " (" + str_soluong + " hồ sơ)";
                    _childnode.IsExpanded = true;
                    _node.Items.Add(_childnode); //them nut con
                    strnutcon = str_ma_sogcs;
                }
                #endregion
            }            
        }

        public void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckBox chk = (CheckBox)sender;
                TreeViewItem itemselect = trvBBAN.SelectedItem as TreeViewItem;               
                // cắt mảng tag
                string[] m = chk.Tag.ToString().Split('@');
                if (chk.IsChecked == true)
                {
                    //kiem tra xem root hay leaf duoc check
                    //#region Check All ChildNode
                    foreach (TreeViewItem supernode in trvBBAN.Items)
                    {
                        checkList(supernode, chk);
                    }
                    foreach (TreeViewItem supernode in trvBBAN.Items)
                    {
                        RefreshCheckBoxList(supernode);
                    }
                    dsResult = new DataSet();
                    // nếu click nút tổng
                    if (chk.Tag.ToString() == "@N@NUTGOC")
                    {
                        ds_Ketqua.Tables["CN"].Rows.Clear();
                        dsResult = ds_BBTHUNGAN;
                        copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                        ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";
                    }

                    // nếu click nút cha
                    if (m[2].ToString() == "NUTCHA")
                    {
                        // lấy dữ liệu của nút cha                            
                        dsResult = GlobalServices.Service_NPCUS.CN_So_BBGiao_Nutcha(strUsername.ToUpper(), Convert.ToInt32(m[0].ToString()), strMadviqly);
                        if (ds_Ketqua.Tables.Count == 0)
                        {
                            if (dsResult != null)
                            {
                                copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                                ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";
                            }
                        }
                        else if(ds_Ketqua.Tables["CN"].Rows.Count == 0)// chưa có hóa đơn nào
                        {
                            if (dsResult != null)
                            {
                                copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                                ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";                                
                            }
                        }
                        else
                        {
                            for (int i = ds_Ketqua.Tables["CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_Ketqua.Tables["CN"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                                {
                                    ds_Ketqua.Tables["CN"].Rows[i].Delete();
                                }
                            } //end of for loop     
                            ds_Ketqua.AcceptChanges();
                            copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                            ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";
                        }
                    }
                    // nếu click nút con
                    if (m[2].ToString() == "NUTCON")
                    {
                        // lấy dữ liệu của nút cha     
                        dsResult = GlobalServices.Service_NPCUS.CN_So_BBGiao_Nutcon(strUsername.ToUpper(), Convert.ToInt32(m[1].ToString()), m[0].ToString().Trim(), strMadviqly);
                        if (ds_Ketqua.Tables.Count == 0)
                        {
                            if (dsResult != null)
                            {
                                copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                                ds_BBTHUNGAN.Tables[0].DefaultView.RowFilter = "1=1";
                            }
                        }
                        else if (ds_Ketqua.Tables["CN"].Rows.Count == 0)// chưa có hóa đơn nào
                        {
                            //ds_Ketqua = new DataSet();
                            if (dsResult != null)
                            {
                                copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                                ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";                                
                            }                           
                        }
                        else
                        {
                            copy_dataset(dsResult, ds_Ketqua.Tables["CN"]);
                            ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";                            
                        }
                    }
                }
                else
                {
                    //DataRow dr =null;
                    foreach (TreeViewItem supernode in trvBBAN.Items)
                    {
                        UncheckList(supernode, chk);
                    }
                    foreach (TreeViewItem supernode in trvBBAN.Items)
                    {
                        RefreshCheckBoxList(supernode);
                    }
                    if (chk.Tag.ToString() == "@N@NUTGOC")
                    {
                        ds_Ketqua.Tables["CN"].Rows.Clear();                        
                    }
                    if (m[2].ToString() == "NUTCHA")
                    {
                        for (int i = ds_Ketqua.Tables["CN"].Rows.Count - 1; i >= 0; i--)
                        {
                            if (ds_Ketqua.Tables["CN"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                            {
                                ds_Ketqua.Tables["CN"].Rows[i].Delete();
                            }
                        } //end of for loop     

                        ds_Ketqua.AcceptChanges();
                        ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";
                    }
                    if (m[2].ToString() == "NUTCON")
                    {
                        for (int i = ds_Ketqua.Tables["CN"].Rows.Count - 1; i >= 0; i--)
                        {
                            if (ds_Ketqua.Tables["CN"].Rows[i]["SO_BBGIAO"].ToString() == m[1].ToString() && ds_Ketqua.Tables["CN"].Rows[i]["MA_SOGCS"].ToString() == m[0].ToString())
                            {
                                ds_Ketqua.Tables["CN"].Rows[i].Delete();
                            }
                        } //end of for loop     

                        ds_Ketqua.AcceptChanges();
                        ds_Ketqua.Tables["CN"].DefaultView.RowFilter = "1=1";                        
                    }
                    chk.Cursor = null;
                }
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show(ex.ToString());
                return;

            }
            this.Cursor = Cursors.Arrow;
        }

        private void checkList(TreeViewItem nodes, CheckBox chk)
        {
            if ((CheckBox)nodes.Header == chk)
            {
                CheckAllChildsWhenParentCheck(nodes);
                return;
            }
            foreach (TreeViewItem node in nodes.Items)
            {
                if (node.Header == chk)
                {
                    if (node.Items.Count > 0)
                    {

                        foreach (TreeViewItem subNode in node.Items)
                        {
                            //check all Node
                            CheckBox chkNode = (CheckBox)subNode.Header;
                            chkNode.IsChecked = true;
                            if (subNode.Items.Count > 0)
                            {
                                checkList(subNode, chk);
                            }
                        }
                        CheckParentWhenAllChildChecked(nodes);
                    }
                    else
                    {
                        CheckParentWhenAllChildChecked(nodes);
                    }
                }
                else
                {
                    checkList(node, chk);
                }

            }
            //dt.AcceptChanges();
        }

        private void UncheckList(TreeViewItem nodes, CheckBox chk)
        {
            //dt.Rows.Clear();
            if ((CheckBox)nodes.Header == chk)
            {
                UnCheckAllChildsWhenParentUnCheck(nodes);
                return;
            }
            foreach (TreeViewItem node in nodes.Items)
            {
                if (node.Header == chk)
                {
                    UnCheckParent(node);
                    if (node.Items.Count > 0)
                    {
                        foreach (TreeViewItem subNode in node.Items)
                        {
                            //check all Node
                            CheckBox chkNode = (CheckBox)subNode.Header;
                            chkNode.IsChecked = false;
                            if (subNode.Items.Count > 0)
                            {
                                UncheckList(subNode, chk);
                            }
                            //load database of childnode
                            //KiemTra_Insert_tbl_All_TheoDoiNo(chkChildNode);
                        }
                    }
                    else
                    {
                        CheckBox chkNode = (CheckBox)node.Header;
                        chkNode.IsChecked = false;
                    }
                }
                else
                {
                    UncheckList(node, chk);
                }
            }
        }

        private void UnCheckParent(TreeViewItem node)
        {
            TreeViewItem parentNode = node.Parent as TreeViewItem;
            if (parentNode != null)
            {
                CheckBox chkParentNode = (CheckBox)parentNode.Header;
                chkParentNode.IsChecked = false;
                UnCheckParent(parentNode);
            }
        }

        private void RefreshCheckBoxList(TreeViewItem nodes)
        {
            DataRow[] dr = null;
            foreach (TreeViewItem subNode in nodes.Items)
            {
                //check all Node
                CheckBox chkNode = (CheckBox)subNode.Header;
                if (subNode.Items.Count > 0)
                {
                    RefreshCheckBoxList(subNode);
                }
                else
                {
                    if (chkNode.IsChecked == true)
                    {
                       
                    }
                    else
                    {
                        
                    }
                }
            }
        }

        private void CheckAllChildsWhenParentCheck(TreeViewItem nodes)
        {
            foreach (TreeViewItem node in nodes.Items)
            {
                CheckBox chkNode = (CheckBox)node.Header;
                chkNode.IsChecked = true;
                CheckAllChildsWhenParentCheck(node);
            }
        }

        private void UnCheckAllChildsWhenParentUnCheck(TreeViewItem nodes)
        {
            foreach (TreeViewItem node in nodes.Items)
            {
                CheckBox chkNode = (CheckBox)node.Header;
                chkNode.IsChecked = false;
                UnCheckAllChildsWhenParentUnCheck(node);
            }
        }

        private void CheckParentWhenAllChildChecked(TreeViewItem nodes)
        {
            int count = 0;
            foreach (TreeViewItem node in nodes.Items)
            {
                CheckBox chkNode = (CheckBox)node.Header;
                if (chkNode.IsChecked == false)
                {
                    count = count + 1;
                    break;
                }
            }
            if (count == 0)
            {
                CheckBox chkNode = (CheckBox)nodes.Header;
                chkNode.IsChecked = true;
                TreeViewItem parentNode = nodes.Parent as TreeViewItem;
                if (parentNode != null)
                {

                    CheckParentWhenAllChildChecked(parentNode);
                }
            }
        }

        private void copy_dataset(DataSet dsnguon, DataTable dt)
        {
            try
            {
                DataRow[] rgINS = dsnguon.Tables[0].Select();
                for (int i = 0; i < rgINS.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["so_BBGIAO"] = rgINS[i]["so_BBGIAO"];
                    dr["Ma_SoGCS"] = rgINS[i]["Ma_SoGCS"];
                    dr["TIEN_NO"] = rgINS[i]["TIEN_NO"];
                    dr["THUE_NO"] = rgINS[i]["THUE_NO"];
                    dr["ID_HDON"] = rgINS[i]["ID_HDON"];                    
                    dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                    dr["LOAI_HDON"] = rgINS[i]["LOAI_HDON"];
                    dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                    dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                    dr["TONG_TIEN"] = rgINS[i]["TONG_TIEN"];
                    dr["KY"] = rgINS[i]["KY"];
                    dr["THANG"] = rgINS[i]["THANG"].ToString();
                    dr["NAM"] = rgINS[i]["NAM"].ToString();
                    dt.Rows.Add(dr);
                    ds_Ketqua.AcceptChanges();
                }
            }
            catch (Exception ex)
            {
                showErrorMess(ex.ToString());
            }
        }

        private void btnIn_Click(object sender, RoutedEventArgs e)
        {
            string strMatngan = strUsername.ToUpper();
            string ten_tngan="";
            DataSet data = new DataSet();
            data = GlobalServices.Service_NPCUS.Ten_Tngan(strMadviqly,strUsername.ToUpper());
            if (data.Tables[0].Rows.Count > 0) ten_tngan = data.Tables[0].Rows[0]["TEN_TNGAN"].ToString(); 
            try
            {
                if (rbtDS_TonCuoiThang.IsChecked == true)
                {                        
                    if (In == 1)
                    {                            
                        BaocaoNPC.Usercontrols.reports.RP_DSTon DSTON = new BaocaoNPC.Usercontrols.reports.RP_DSTon();
                        DSTON.SetDataSource(ds_Ketqua.Tables["CN"]);
                        DSTON.SetParameterValue("ThuNgan", strMatngan);
                        DSTON.SetParameterValue("Ten_TNgan", ten_tngan);
                        DSTON.SetParameterValue("pTendienluc", strTen_dviqly);
                        DSTON.SetParameterValue("pTencongty", strTen_dviqly_CT);
                        UtilReportOMS.RptManager.showReportToWin(this, DSTON, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                    }
                }
                else
                {
                    if (In == 2)
                    {
                        BaocaoNPC.Usercontrols.reports.RP_DS_NopATM report = new BaocaoNPC.Usercontrols.reports.RP_DS_NopATM();
                        DataSet ds = GlobalServices.Service_NPCUS.ATM_Ton_Select(strMatngan.ToUpper(), strMadviqly);
                        if (ds != null)
                        {
                            report.SetDataSource(ds.Tables[0]);
                            report.SetParameterValue("ThuNgan", strMatngan);
                            report.SetParameterValue("Ten_TNgan", ten_tngan);
                            report.SetParameterValue("pTendienluc", strTen_dviqly);
                            report.SetParameterValue("pTencongty", strTen_dviqly_CT);
                            UtilReportOMS.RptManager.showReportToWin(this, report, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
            
        }

        private void dbt_DS_NoATM_Click(object sender, RoutedEventArgs e)
        {
            Kiemtra();
            In = 2;
        }

        private void rbtDS_TonCuoiThang_Click(object sender, RoutedEventArgs e)
        {
            Kiemtra();
            In = 1;
        }

        public void showErrorMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }
    }
}
